

from itsdangerous.jws import TimedJSONWebSignatureSerializer
from itsdangerous.jws import TimedJSONWebSignatureSerializer
from django.conf import settings


#1.加密openid
def encode_openid(openid):
    #1.创建加密对象
    serializer=TimedJSONWebSignatureSerializer(secret_key=settings.SECRET_KEY,expires_in=300)


    #2.加密数据
    token = serializer.dumps({"openid":openid})

    #3.返回响应,得到的结果是二进制需要解密
    return token.decode()







#2.解密openid
def decode_openid(data):
    # 1.
    serializer = TimedJSONWebSignatureSerializer(secret_key=settings.SECRET_KEY, expires_in=300)

    # 2.为了防止因为解密时间过期报错,要用try.
    try:
        dict_data = serializer.loads(data)
    except Exception as e:
        return None

    # 3.返回响应,
    return dict_data.get("openid")
